//
//  ActivityViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "ActivityViewController.h"
#import "ACModel.h"
#import "ACLLModel.h"
#import "ACCollectionViewcell.h"
#import "ACSViewController.h"
@interface ActivityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *ACCollection;
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)NSMutableArray *allFCArray;
@property(nonatomic,strong)ACModel *acModel;
@end

@implementation ActivityViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor wheatColor];
    self.allArray = [NSMutableArray array];
    self.allFCArray = [NSMutableArray array];
    //
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.itemSize = CGSizeMake( 375/3.00* WIDTHSCALE, 375/3.00 *HEIGHTSCALE);
    flowLayout.sectionInset = UIEdgeInsetsMake(0 , 0, 0, 0);
    //
    self.ACCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];
    self.ACCollection.backgroundColor = [UIColor wheatColor];
    //协议
    self.ACCollection.delegate = self;
    self.ACCollection.dataSource = self;
    [self.view addSubview:self.ACCollection];
    //注测cell
    [self.ACCollection registerClass:[ACCollectionViewcell class] forCellWithReuseIdentifier:@"cell"];
    //注册表头
    [self.ACCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    flowLayout.headerReferenceSize=CGSizeMake(WIDTH, 60 * HEIGHTSCALE);
    [self AFNWroking];
    [self addTabBar];

}
-(void)addTabBar
{
    //自定义UINavigationBar的标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50*WIDTHSCALE, 0, 150*WIDTHSCALE, 44*HEIGHTSCALE)];
    titleLabel.text = @"家居分类";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:20];
    self.navigationItem.titleView = titleLabel;


}
-(void)AFNWroking
{
    NSString *url = [NSString stringWithFormat:@"http://api.bohejiaju.com/v1/channel_groups/all"];
    [BlockGetAFNYiBu GetAfnURLWithString:url resultBlock:^(id result)
    {
        NSMutableDictionary *BigDic = (NSMutableDictionary *)result;
        NSMutableDictionary *dicD = [BigDic objectForKey:@"data"];
        NSMutableArray *array = [dicD objectForKey:@"channel_groups"];
        for (NSMutableDictionary *dic in array)
        {
            self.acModel = [[ACModel alloc]init];
            
            
            [self.acModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:self.acModel];
            NSLog(@"sourcename=%@",self.acModel.name);
//            NSMutableArray *arrayFC = [dic objectForKey:@"channels"];
//            for (NSMutableDictionary *dicFC in arrayFC)
//            {
//                ACLLModel *modelFC = [[ACLLModel alloc]init];
//                modelFC.icon_url = [dicFC objectForKey:@"icon_url"];
//                modelFC.name = [dicFC objectForKey:@"name"];
//                modelFC.item_count = [dicFC objectForKey:@"item_count"];
//                modelFC.id = [[dicFC objectForKey:@"id"]integerValue];
//                
//                [self.allFCArray addObject:modelFC];
//            }
        }
        [self.ACCollection reloadData];
    } errorBlcok:^(NSError *error)
    {
    }];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionVi
{
    
    return self.allArray.count;
}
//
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        ACModel *acmodel = self.allArray[0];
        NSLog(@"model=%@",acmodel.channels);
      return   acmodel.channels.count;
    }
    else
    {
        ACModel *acmodel = self.allArray[1];
        NSLog(@"modle==1%@",acmodel.channels);
        return   acmodel.channels.count;
    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ACCollectionViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [ UIColor clearColor];
    if (indexPath.section == 0) {
        
        ACModel *acmodel = self.allArray[0];
        NSMutableArray *tmp=[NSMutableArray array];
        NSMutableArray *tmp1=[NSMutableArray array];
        for (NSMutableDictionary *dicFC in acmodel.channels)
        {
            ACLLModel *modelFC = [[ACLLModel alloc]init];
            modelFC.icon_url = [dicFC objectForKey:@"icon_url"];
            modelFC.name = [dicFC objectForKey:@"name"];
            modelFC.item_count = [dicFC objectForKey:@"item_count"];
            modelFC.ID = [[dicFC objectForKey:@"id"]integerValue];
            [tmp addObject:modelFC];
//            [self.allFCArray addObject:modelFC];
        }
//        self.allFCArray=tmp;
        tmp1=tmp;
        ACLLModel *model = [tmp1 objectAtIndex:indexPath.row];
        cell.model = model;
        
    }
    if (indexPath.section == 1) {
        
        
        ACModel *acmodel = self.allArray[1];
        NSMutableArray *tmp=[NSMutableArray array];
        NSMutableArray *tmp1=[NSMutableArray array];
        for (NSMutableDictionary *dicFC in acmodel.channels)
        {
            ACLLModel *modelFC = [[ACLLModel alloc]init];
            modelFC.icon_url = [dicFC objectForKey:@"icon_url"];
            modelFC.name = [dicFC objectForKey:@"name"];
            modelFC.item_count = [dicFC objectForKey:@"item_count"];
            modelFC.ID = [[dicFC objectForKey:@"id"]integerValue];
            
            [self.allFCArray addObject:modelFC];
//            [tmp addObject:acmodel];
        }
//        self.allFCArray=tmp;
//        tmp1=tmp;
        ACLLModel *model = [self.allFCArray objectAtIndex:indexPath.row];
        cell.model = model;
    }

    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    ACLLModel *model = [self.allFCArray objectAtIndex:indexPath.row];
    ACSViewController *acsVC = [[ACSViewController alloc]init];
    acsVC.ID = model.ID;
    [self.navigationController pushViewController:acsVC animated:YES];
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableName = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    UILabel *labelName = [[UILabel alloc]initWithFrame:CGRectMake(10 * WIDTHSCALE, 5 * HEIGHTSCALE, WIDTH, 30 * HEIGHTSCALE)];
    labelName.font = [UIFont fontWithName:@"Arial" size:14];
    
    NSLog(@"===%@",self.acModel.name);
    
    
    
    
    labelName.text = self.acModel.name;
    [reusableName addSubview:labelName];
    
    return reusableName;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
